package com.example.famhealing.timeline;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.famhealing.R;

public class HealingTimelineActivity extends Activity implements
		OnClickListener {

	private ArrayList<HealingtalkList> array;
	private HealingtalkList data;
	private HealingtalkListAdapter adapter;

	ListView healingtalkList;

	String healingtalkUrl = "http://192.168.0.61:8080/famhealing/getFamHealingtalk.get?phone_num=01011111111";

	String date = "";
	String user_name = "";
	String content = "";

	boolean focusOnET = false;
	static ImageButton confirm;
	static CustomEditText myhealingtalk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_healing_timeline);

		ImageButton conditionButton = (ImageButton) findViewById(R.id.timeline_condition);
		ImageButton scrapButton = (ImageButton) findViewById(R.id.timeline_scrap);
		conditionButton.setOnClickListener(this);
		scrapButton.setOnClickListener(this);
		
		confirm = (ImageButton) findViewById(R.id.confirm);
		myhealingtalk = (CustomEditText) findViewById(R.id.editHealingtalk);
		array = getHealingtalk();

		myhealingtalk.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if (hasFocus == true) {
					confirm.setVisibility(View.VISIBLE);
					focusOnET = true;
				}
			}
		});
//		myhealingtalk.setOnKeyListener(new OnKeyListener() {
//
//			@Override
//			public boolean onKey(View view, int keyCode, KeyEvent event) {
//				if (event.getAction() == KeyEvent.ACTION_DOWN) {
//					if (keyCode == KeyEvent.KEYCODE_BACK && focusOnET == true) {
//						ImageButton confirm = (ImageButton) findViewById(R.id.confirm);
//						confirm.setVisibility(View.GONE);
//						myhealingtalk.clearFocus();
//						focusOnET = false;
//						System.out.println("asdfasdfddddd");
//						LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
//						ll1.requestFocus();
//					}
//				}
//				return false;
//			}
//		});
		getMyHealingtalk();

		healingtalkList = (ListView) findViewById(R.id.healingtalk_list);
		adapter = new HealingtalkListAdapter(this,
				android.R.layout.simple_list_item_1, array);
		healingtalkList.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.healing_timeline, menu);
		return true;
	}

	public ArrayList<HealingtalkList> getHealingtalk() {
		ArrayList<HealingtalkList> list = new ArrayList<HealingtalkList>();

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.permitNetwork().build());

		try {
			URL url1 = new URL(healingtalkUrl);
			InputStream in = url1.openStream();

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();

			parser.setInput(in, "euc-kr");

			int eventType = parser.getEventType();
			boolean isItemTag = false;
			String tagName = "";
			boolean check = false;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					tagName = parser.getName();
					if (!tagName.equals("data")
							&& !tagName.equals("healingtalk"))
						isItemTag = true;
				} else if (eventType == XmlPullParser.TEXT && isItemTag == true) {
					if (tagName.equals("user_name")) {
						user_name = parser.getText();
					}
					if (tagName.equals("date")) {
						date = parser.getText();
					}
					if (tagName.equals("content")) {
						content = parser.getText();
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					isItemTag = false;
					if (tagName.equals("content") && check == false) {
						data = new HealingtalkList(date, "", user_name, content);
						list.add(data);
						check = true;
					} else if (!tagName.equals("diagnose_check")
							&& check == true)
						check = false;
				}

				eventType = parser.next();
			}
		} catch (Exception e) {
			System.out.println(e);
		}

		return list;
	}

	public void getMyHealingtalk() {
		String state_message = "";
		healingtalkUrl = "http://192.168.0.61:8080/famhealing/getUser.get?phone_num=01011111111";

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.permitNetwork().build());

		try {
			URL url1 = new URL(healingtalkUrl);
			InputStream in = url1.openStream();

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();

			parser.setInput(in, "euc-kr");

			int eventType = parser.getEventType();
			boolean isItemTag = false;
			String tagName = "";
			boolean check = false;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					tagName = parser.getName();
					if (!tagName.equals("data") && !tagName.equals("user"))
						isItemTag = true;
				} else if (eventType == XmlPullParser.TEXT && isItemTag == true) {
					if (tagName.equals("state_message")) {
						state_message = parser.getText();
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					isItemTag = false;
				}

				eventType = parser.next();
			}
		} catch (Exception e) {
			System.out.println(e);
		}

		myhealingtalk.setText(state_message);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.timeline_condition:
			intent = new Intent(getBaseContext(), TimelineActivity.class);
			startActivity(intent);
			overridePendingTransition(0,0);
			break;
		case R.id.timeline_scrap:
			intent = new Intent(getBaseContext(), ScrapTimelineActivity.class);
			startActivity(intent);
			overridePendingTransition(0,0);
			break;
		}
	}
	
	public void invisible()
	{
		//confirm=(ImageButton)findViewById(R.id.confirm);
		//myhealingtalk=(CustomEditText)findViewById(R.id.editHealingtalk);
		myhealingtalk.clearFocus();
		focusOnET = false;
		confirm.setVisibility(View.GONE);
	}
	
}
